package obstaculos;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

public class PlataformasCiclicas extends Obstaculo{

	private BitmapTextureAtlas mapaDeTexturas;
	private TextureRegion regiaoDaTextura;
	
	int timerAtivacao = 0;
	
	Rectangle areaTotal;
	
	public PlataformasCiclicas(int x, int y, Context contexto, TextureManager tm) {
		this.x = x;
		this.y = y;
		largura = 64;
		altura = 128;
		
		mapaDeTexturas = new BitmapTextureAtlas(128, 64, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		regiaoDaTextura = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mapaDeTexturas, contexto, "Plataformas ciclicas.png", 0, 0);
		tm.loadTexture(mapaDeTexturas);
		
		sprite = new Sprite(x, y, regiaoDaTextura);
		//spriteGatilho = new Sprite(xGatilho, yGatilho, regiaoDaTextura);
		listaDePaths[0][0] = x; listaDePaths[1][0] = y+192;
		sprites.add(new Sprite(listaDePaths[0][0], listaDePaths[1][0], regiaoDaTextura));
		listaDePaths[0][1] = x+64; listaDePaths[1][1] = y+320;
		sprites.add(new Sprite(listaDePaths[0][1], listaDePaths[1][1], regiaoDaTextura));
		listaDePaths[0][2] = x+192; listaDePaths[1][2] = y+320;
		sprites.add(new Sprite(listaDePaths[0][2], listaDePaths[1][2], regiaoDaTextura));
		listaDePaths[0][3] = x+256; listaDePaths[1][3] = y+192;
		sprites.add(new Sprite(listaDePaths[0][3], listaDePaths[1][3], regiaoDaTextura));
		listaDePaths[0][4] = x+192; listaDePaths[1][4] = y+64;
		sprites.add(new Sprite(listaDePaths[0][4], listaDePaths[1][4], regiaoDaTextura));
		listaDePaths[0][5] = x+64; listaDePaths[1][5] = y+64;
		sprites.add(new Sprite(listaDePaths[0][5], listaDePaths[1][5], regiaoDaTextura));
		listaDePosicoes[0][0] = listaDePaths[0][0]; listaDePosicoes[1][0] = listaDePaths[1][0];
		listaDePosicoes[0][1] = listaDePaths[0][1]; listaDePosicoes[1][1] = listaDePaths[1][1];
		listaDePosicoes[0][2] = listaDePaths[0][2]; listaDePosicoes[1][2] = listaDePaths[1][2];
		listaDePosicoes[0][3] = listaDePaths[0][3]; listaDePosicoes[1][3] = listaDePaths[1][3];
		listaDePosicoes[0][4] = listaDePaths[0][4]; listaDePosicoes[1][4] = listaDePaths[1][4];
		listaDePosicoes[0][5] = listaDePaths[0][5]; listaDePosicoes[1][5] = listaDePaths[1][5];
	}

	@Override
	public void atualiza() {
		ciclo++;
		if(ciclo%5 == 0){
			for(int i = 0; i < 6; i++){
				if(posicoesObjetivo[i] == 5){
					listaDePaths[0][i] -= 1;
					listaDePaths[1][i] += 2;
				}
				if(posicoesObjetivo[i] == 4){
					listaDePaths[0][i] -= 2;
				}
				if(posicoesObjetivo[i] == 3){
					listaDePaths[0][i] -= 1;
					listaDePaths[1][i] -= 2;
				}
				if(posicoesObjetivo[i] == 2){
					listaDePaths[0][i] += 1;
					listaDePaths[1][i] -= 2;
				}
				if(posicoesObjetivo[i] == 1){
					listaDePaths[0][i] += 2;
				}
				if(posicoesObjetivo[i] == 0){
					listaDePaths[0][i] += 1;
					listaDePaths[1][i] += 2;
				}
			}
			
			for(int i = 0; i < 6; i++){
				if(posicoesObjetivo[i] == 5){
					if(listaDePaths[0][i] == 1952 && listaDePaths[1][i] == 97){
						posicoesObjetivo[i] = 0;
					}
				}
				if(posicoesObjetivo[i] == 4){
					if(listaDePaths[0][i] == 1984 && listaDePaths[1][i] == 33){
						posicoesObjetivo[i] = 5;
					}
				}
				if(posicoesObjetivo[i] == 3){
					if(listaDePaths[0][i] == 2080 && listaDePaths[1][i] == 33){
						posicoesObjetivo[i] = 4;
					}
				}
				if(posicoesObjetivo[i] == 2){
					if(listaDePaths[0][i] == 2112 && listaDePaths[1][i] == 97){
						posicoesObjetivo[i] = 3;
					}
				}
				if(posicoesObjetivo[i] == 1){
					if(listaDePaths[0][i] == 2080 && listaDePaths[1][i] == 161){
						posicoesObjetivo[i] = 2;
					}
				}
				if(posicoesObjetivo[i] == 0){
					if(listaDePaths[0][i] == 1984 && listaDePaths[1][i] == 161){
						posicoesObjetivo[i] = 1;
					}
				}
				ciclo = 0;
			}
		}
		
		//for(int i = 0; i < sprites.size(); i++){
			//sprites.get(i).setPosition(listaDePaths[0][i], listaDePaths[1][i]);
		//}
		/*if(timerAtivacao < 10){
			frame = 0;
		}
		if(timerAtivacao > 10 && timerAtivacao < 20){
			frame = 1;
		}
		if(timerAtivacao == 20){
			frame = 2;
		}
		if(ativado){
			if(timerAtivacao<20){
				timerAtivacao++;
			}
		}else{
			if(timerAtivacao>0){
				timerAtivacao--;
			}
		}*/
		//frame = 2;
	}

	@Override
	public boolean colidiuComDono(AnimatedSprite spriteDono) {
		return false;
	}

	@Override
	public void posicionaNaCena(int mapx, int mapy) {
		for(int i = 0; i < sprites.size(); i++){
			sprites.get(i).setPosition(listaDePaths[0][i]-mapx, listaDePaths[1][i]-mapy);
		}
	}
}
